package com.kkd.kkdapp.activity;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.android_async_http.AsyncHttpResponseHandler;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;


import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

public class UpdateWithdrawSucceedActivity extends BaseActivity implements
		OnClickListener {

	private EditText updatewithdraw_ps; // 新密码
	private EditText updatesetdeal_ed; // 确认密码
	// private String version; // 当前版本号
	private Button update_bt; // 提交
	private RelativeLayout find_img; // 返回
	private boolean BTFLAG;
	private boolean SUBMITFLAG = true;
	private Handler mHandler = new Handler();
	private Dialog loadDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_withdraw_succeed);
		initView();
		setListener();
	}
	
	@Override
	protected void onDestroy() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		super.onDestroy();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		updatewithdraw_ps = (EditText) this
				.findViewById(R.id.updatewithdraw_ps);
		updatesetdeal_ed = (EditText) this.findViewById(R.id.updatesetdeal_ed);
		update_bt = (Button) this.findViewById(R.id.update_bt);
		find_img = (RelativeLayout) this.findViewById(R.id.find_img);
	}

	/**
	 * 注册监听事件
	 */
	public void setListener() {
		update_bt.setOnClickListener(this);
		find_img.setOnClickListener(this);
		updatewithdraw_ps.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if ("".equals(updatewithdraw_ps.getText().toString())
						|| "".equals(updatesetdeal_ed.getText().toString())) {
					update_bt.setEnabled(false);
					BTFLAG = false;
				} else {
					update_bt.setEnabled(true);
					BTFLAG = true;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		updatesetdeal_ed.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if ("".equals(updatewithdraw_ps.getText().toString())
						|| "".equals(updatesetdeal_ed.getText().toString())) {
					update_bt.setEnabled(false);
					BTFLAG = false;
				} else {
					update_bt.setEnabled(true);
					BTFLAG = true;
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.update_bt:
			if (SUBMITFLAG) {
				SUBMITFLAG = false;
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						SUBMITFLAG = true;
					}
				}, 2000);
				if (BTFLAG) {
					if (updatewithdraw_ps.getText().toString()
							.equals(updatesetdeal_ed.getText().toString())) {
						if (userstringFilter(updatewithdraw_ps.getText()
								.toString())) {
							findPassword();
						} else {
							showToast(R.string.new_password);
						}
					} else {
						showToast(R.string.registerActivity_button);
					}

				}
			}
			break;
		case R.id.find_img:
			finish();
			break;

		}

	}

	/**
	 * 找回支付密码
	 */
	public void findPassword() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(this,  "努力加载中....");

		KkdHttpMagager.getInstance().getFindPasswordManager( 
				updatewithdraw_ps.getText().toString(),
				updatesetdeal_ed.getText().toString(),  String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						PreferenceUtils.getInstance().setWithdrawPassWord(
								UpdateWithdrawSucceedActivity.this, -1);
						finish();
						if (loadDialog != null) {
							loadDialog.dismiss();
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
						if (loadDialog != null) {
							loadDialog.dismiss();
						}
					}
				});

		// KkdHttpMagager.getInstance().getFindPasswordManager(SystemUtil.getVersionName(this),
		// cat[1],
		// updatewithdraw_ps.getText().toString(), updatesetdeal_ed
		// .getText().toString(), cat[0],
		// String.class,new HttpCallBackT() {
		//
		// @Override
		// public void onSuccess(int statusCode, Object res) {
		//
		// try {
		// String responseString = res.toString();
		// JSONObject request = new JSONObject(responseString);
		// if (request.getInt("code") == 0) {
		// PreferenceUtils
		// .getInstance()
		// .setWithdrawPassWord(
		// UpdateWithdrawSucceedActivity.this,
		// -1);
		// finish();
		// } else {
		// Toasttool.MyToast(
		// UpdateWithdrawSucceedActivity.this,
		// request.getString("desc"));
		// }
		// } catch (JSONException e) {
		//
		// e.printStackTrace();
		// }
		//
		// AppUtil.stopProgressDialog();
		// }
		//
		// @Override
		// public void onFailure(int statusCode,
		// byte[] responseBody, Throwable error) {
		//
		// AppUtil.stopProgressDialog();
		// }
		// });
	}

}
